package com.ccjq.jq173.shosestore.web.controller.order;

import com.ccjq.jq173.shosestore.core.util.PageBean;
import com.ccjq.jq173.shosestore.core.util.PageHelper;
import com.ccjq.jq173.shosestore.member.po.User;
import com.ccjq.jq173.shosestore.order.po.Order;
import com.ccjq.jq173.shosestore.order.po.OrderProduct;
import com.ccjq.jq173.shosestore.order.service.OrderService;
import com.ccjq.jq173.shosestore.order.service.impl.OrderServiceImpl;
import com.ccjq.jq173.shosestore.product.service.impl.productServiceImpl;
import com.ccjq.jq173.shosestore.product.service.productService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends HttpServlet {
    private OrderService orderService=null;
    private static final String FLAG_ORDER_LIST="fol";
    private static final String FLAG_ONE_ORDER="foo";
    private static final String FLAG_PAGE="page";
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("分页操作");
        String flag=request.getParameter("flag");
        orderService=new OrderServiceImpl();
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        if(session.getAttribute("ys")==null)
        {
            session.setAttribute("ys","4");
        }
        if(user==null)
        {
            System.out.println(1);
            request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
            return;
        }
        if(FLAG_ORDER_LIST.equals(flag))
        {

            orderList(request,response);
        }
        else if(FLAG_ONE_ORDER.equals(flag))
        {
            oneOrder(request,response);
        }
        else if(FLAG_PAGE.equals(flag))
        {

        }
    }

    private void orderList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        System.out.println("表单进入");
        String userid=((User)request.getSession().getAttribute("user")).getUserId();
        //String userid="rrrr";
        HttpSession session=request.getSession();
        int count=orderService.getCount(userid);
        String ys=(String)session.getAttribute("ys");
        String pageSize=request.getParameter("ps")==null?ys:request.getParameter("ps");
        System.out.println(pageSize);
        session.setAttribute("ys",pageSize);
        PageBean pageBean= PageHelper.getPager(request,count,Integer.parseInt(pageSize));
        List<Order> list=orderService.getPageList(pageBean.getStartRow(),pageBean.getPageSize(),userid);
        request.setAttribute("pageBean",pageBean);
        //List<Order> list=orderService.queryAllOrders("rrrr");
        //System.out.println(list);
        //System.out.println(((User) request.getSession().getAttribute("user")).getUserId());
        request.setAttribute("orderList",list);
        request.getRequestDispatcher("/WEB-INF/jsp/order/orderlist.jsp").forward(request,response);

    }
    private void oneOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String orderId=request.getParameter("orderId");
        Order order=orderService.queryOneOrder(orderId);
        request.setAttribute("oneOrder",order);
        List<OrderProduct> op=orderService.queryBuylog(orderId);
        productService  pro=new productServiceImpl();
        for(int i=0;i<op.size();i++)
        {
            OrderProduct or=op.get(i);
            String feature=pro.get(or.getProductId());
            System.out.println("feature:"+feature);
            or.setFeature(feature);
            op.set(i,or);

        }
        request.setAttribute("buylog",op);
        request.getRequestDispatcher("/WEB-INF/jsp/order/oneorder.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
